﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.BizTalk.Bam.EventObservation;
using Software41.Tracking.BAM.Adapters;
using System.Configuration;
using Software41.Tracking.BAM;

namespace Tracking.ConsoleUI
{
    class Program
    {
        static void Main(string[] args)
        {
            //verify that we have a BAM connection string
            var connStr = ConfigurationManager.ConnectionStrings["BAMTesting"].ConnectionString;

            var bufferedEventStrm = new BufferedEventStream(connStr, 0);
            IEventStream eventStream = new EventStreamAdapter(bufferedEventStrm);


            ProcessTraceInfo preprocess = new ProcessTraceInfo("Pre-Publishing", Guid.NewGuid().ToString(), eventStream);
            preprocess.Begin();
            preprocess.End();

            string processId = Guid.NewGuid().ToString();
            Console.WriteLine(string.Format("The ProcessId is {0}", processId));

            ProcessTraceInfo process = new ProcessTraceInfo("Publishing", processId, eventStream);
            process.Begin("ProofRequestDto", "<PublishRequestDto>We are starting!!!</PublishRequestDto>");
            process.AddAssociatedProcess(preprocess.ProcessInfoId);

            var logDetail = process.AddLogDetail();
            logDetail.AddDetail("Test log 1");
            logDetail.AddDetail("Test log 2");


            #region Add a StepInfo which has two ActionInfos.  The first completed successfully, the second has a handled exception

            StepInfo step = new StepInfo("Publishing Step 1", process.ProcessInfoId, eventStream);
            step.Begin("Step1Dto", "<Step1Dto>Blah, Blah, Blah</Step1Dto>");

            ActionInfo actionNoEx = step.AddActionInfo("MyServiceCall");
            actionNoEx.Begin("ActionNoEx", "<Request>More Blah, Blah, Blah</Request>");
            actionNoEx.End(true, "ActionNoEx", "<Response>More Blah, Blah, Blah</Response>");

            ActionInfo actionWithEx = step.AddActionInfo("MyServiceCallWithEx");
            try
            {
                actionWithEx.Begin("ActionWithEx", "<Request>More Blah, Blah, Blah</Request>");
                throw new Exception("ActionWithExceptionTest");
            }
            catch (Exception ex)
            {
                actionWithEx.AddException(ex);
                actionWithEx.AddExceptionActionTaken("Action 1");
                actionWithEx.AddExceptionActionTaken("Action 2");
                actionWithEx.AddExceptionActionTaken("Action 3");
                actionWithEx.End(false);
            }

            step.End(true, "Step1Dto - Finish", "<Step1Dto>Finish Blah, Blah, Blah</Step1Dto>");
            #endregion
            eventStream.Flush();

            #region Add a StepInfo which has an ActionInfo that causes an exception not handled at the ActionInfo level
            //
            StepInfo errorStep = new StepInfo("Publishing Step 2", process.ProcessInfoId, eventStream);
            try
            {
                errorStep.Begin("Step2Dto", "<Step2>Error is coming</Step2>");

                ActionInfo errorAction = errorStep.AddActionInfo("ActionWhichThrowsEx");
                errorAction.Begin("ErrorActionRequest", "<Request>Error Action Request</Request>");
                throw new Exception("Here is a step level exception");
            }
            catch (Exception ex)
            {
                errorStep.AddException(ex);
                errorStep.AddExceptionActionTaken("Here is my first action taken");
                errorStep.AddExceptionActionTaken("Here is my second action taken");

            }

            errorStep.End(false, "Step1Dto - Finish", "<Step1Dto>Finish Blah, Blah, Blah</Step1Dto>");

            #endregion


            process.End("PublishRequestDto", "<PublishRequestDto>We are finished!!!</PublishRequestDto>");

            eventStream.Flush();

            Console.WriteLine(string.Format("BAM Activity for Process {0} completed.", processId));
            Console.ReadLine();
        }
    }
}
